﻿Imports System.Reflection

Public Class MainForm

    Dim _配置表 As DataTable

    Private Sub tsb监听数据库设置_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb监听数据库设置.Click

        Using frm As New InitForm
            frm.ShowDialog(Me)
        End Using

    End Sub

    Private Sub tbs设置监听时间_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbs设置监听时间.Click

        Using frm As New SetTimerForm
            frm.ShowDialog(Me)
        End Using

        RefreshLeft()

    End Sub

    Private Sub tbs监听数据库状态_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbs监听数据库状态.Click

        Using frm As New StatusForm
            frm.ShowDialog(Me)
        End Using

    End Sub

    Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If Me.WindowState = FormWindowState.Maximized Then
            MyConfig.最后窗体高度 = ""
            MyConfig.最后窗体宽度 = ""
        Else
            MyConfig.最后窗体高度 = Me.Height
            MyConfig.最后窗体宽度 = Me.Width

            MyConfig.最后上面距离 = Me.Top
            MyConfig.最后左边距离 = Me.Left
        End If
        
    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.StatusStrip1.Visible = False

        If MyConfig.隐藏行头 = "1" Then
            chk隐藏行头.Checked = True
        Else
            chk隐藏行头.Checked = False
        End If

        IListenDb.CommonConUtil.连接字符串 = MyConfig.连接字符串

        Dim iForm As IListenDb.IForm
        Dim assembleName As String

        For Each lst As 组件表 In 组件表操作类.GetAll组件
            assembleName = lst.组件名称

            '开始删除触发器
            iForm = Assembly.Load(assembleName).CreateInstance(assembleName & ".CreateForm")
            If iForm IsNot Nothing AndAlso lst.界面 = 1 Then

                If lst.菜单 = 1 Then
                    iForm.LoadMenu(Me.MenuStrip1, MyConfig.前缀)
                End If

                If lst.工具栏 = 1 Then
                    iForm.LoadToolBar(Me.ToolStrip1, MyConfig.前缀)
                End If

            End If
        Next

        IListenDb.CommonConUtil.连接字符串 = MyConfig.连接字符串

        RefreshLeft()

        RefreshStatus()

        InitTitle()

        MenuStrip1.Visible = False

        If Not (MyConfig.最后窗体高度.Trim = "" OrElse MyConfig.最后窗体宽度.Trim = "") Then
            Me.WindowState = FormWindowState.Normal

            Me.Height = CInt(MyConfig.最后窗体高度)
            Me.Width = CInt(MyConfig.最后窗体宽度)

            If MyConfig.最后上面距离.Trim <> "" Then
                Me.Top = MyConfig.最后上面距离
            End If

            If MyConfig.最后左边距离.Trim <> "" Then
                Me.Left = MyConfig.最后左边距离
            End If

            Me.StartPosition = FormStartPosition.CenterScreen
        Else
            Me.WindowState = FormWindowState.Maximized
        End If

    End Sub

    Private Sub RefreshPosition()

        Dim totalWidth As Integer = 0
        totalWidth = dgLeft.Columns.GetColumnsWidth(DataGridViewElementStates.Displayed)
        If dgLeft.DisplayedRowCount(True) <> dgLeft.RowCount Then
            totalWidth += 20
        Else
            totalWidth += 3
        End If
        SplitContainer1.SplitterDistance = totalWidth

    End Sub

    Private Sub RefreshStatus()

        Dim iForm As IListenDb.IForm
        Dim assembleName As String

        For Each lst As 组件表 In 组件表操作类.GetAll组件
            assembleName = lst.组件名称
            '开始删除触发器
            iForm = Assembly.Load(assembleName).CreateInstance(assembleName & ".CreateForm")
            If iForm IsNot Nothing AndAlso lst.界面 = 1 Then
                iForm.InitForm(Me.MenuStrip1, Me.ToolStrip1, MyConfig.前缀)
            End If
        Next

        If MyConfig.自动刷新 = "0" Then
            tsb自动刷新.ForeColor = Color.Red
        Else
            tsb自动刷新.ForeColor = System.Drawing.SystemColors.ControlText
        End If

    End Sub

    Private Sub RefreshLeft()

        Dim dt As DataTable = IListenDb.CommonConUtil.GetTable("select Key_Timer as 快照时间,Descripion as 快照描述  from " + MyConfig.前缀 + "_Key order by Key_Timer desc ")
        IListenDb.CommonConUtil.CloseCon()

        Dim 所有组件 As List(Of 组件表) = 组件表操作类.GetAll组件
        Dim lstDataNew As IListenDb.IFormatMainForm
        If 所有组件.Count > 0 Then
            For index As Integer = 所有组件.Count - 1 To 0 Step -1
                lstDataNew = Assembly.Load(所有组件(index).组件名称).CreateInstance(所有组件(index).组件名称 & ".FormatMainForm")
                If lstDataNew IsNot Nothing Then
                    lstDataNew.RefreshLeft(dt, dgLeft)
                    Exit For
                End If
            Next
        End If

        RefreshPosition()

    End Sub

    Private Sub InitTitle()

        Dim dt As DataTable = IListenDb.CommonConUtil.GetTable("select Key_Timer as 快照时间,Descripion as 快照描述  from " + MyConfig.前缀 + "_Key order by Key_Timer desc ")
        IListenDb.CommonConUtil.CloseCon()

        Dim title As String = MyConfig.主窗口标题

        Dim 所有组件 As List(Of 组件表) = 组件表操作类.GetAll组件
        Dim lstDataNew As IListenDb.IFormatMainForm
        If 所有组件.Count > 0 Then
            For index As Integer = 所有组件.Count - 1 To 0 Step -1
                lstDataNew = Assembly.Load(所有组件(index).组件名称).CreateInstance(所有组件(index).组件名称 & ".FormatMainForm")
                If lstDataNew IsNot Nothing Then
                    lstDataNew.InitTitle(title)
                    Exit For
                End If
            Next
        End If

        Me.Text = title

    End Sub

    Private Sub RefreshStatusBar(ByVal keyTime As String)

    End Sub

    Private Sub 快照详细信息(ByVal 快照时间 As String, ByVal dt As DataTable)

        Dim 快照时间A As String = Mid(快照时间, 1, 4) & "年" & Mid(快照时间, 5, 2) & "月" & Mid(快照时间, 7, 2) & "日" & Mid(快照时间, 9, 2) & "时" & Mid(快照时间, 11, 2) & "分" & Mid(快照时间, 13, 2) & "秒" & Mid(快照时间, 16, 3) & "毫秒"

        Dim dt快照 As New DataTable

        dt快照.Columns.Add("内容", GetType(String))

        dt快照.Rows.Add(New String() {快照时间A})

        Dim dt表 As DataTable = dt.DefaultView.ToTable("更新表名称", True, "更新表名称")

        If dt表.Rows.Count > 0 Then

            Dim 添加条数 As Integer = 0
            Dim 修改条数 As Integer = 0
            Dim 删除条数 As Integer = 0

            dt快照.Rows.Add(New String() {"共更新了" & dt表.Rows.Count & "个表"})

            For index As Integer = 0 To dt表.Rows.Count - 1
                dt快照.Rows.Add(New String() {CStr(dt表.Rows(index)("更新表名称")).Replace(MyConfig.前缀 & "_", "")})
                Dim drs() As DataRow = dt.Select("更新表名称 ='" & CStr(dt表.Rows(index)("更新表名称")) & "' AND 更新类型='添加'")
                添加条数 = drs.Length

                drs = dt.Select("更新表名称 ='" & CStr(dt表.Rows(index)("更新表名称")) & "' AND 更新类型='修改前'")
                修改条数 = drs.Length

                drs = dt.Select("更新表名称 ='" & CStr(dt表.Rows(index)("更新表名称")) & "' AND 更新类型='删除'")
                删除条数 = drs.Length

                dt快照.Rows.Add(New String() {IIf(添加条数 > 0, " 添加:" & 添加条数 & "条 ", "") & IIf(修改条数 > 0, " 修改:" & 修改条数 & "条 ", "") & IIf(删除条数 > 0, " 删除:" & 删除条数 & "条", "")})
            Next
        End If

        dg快照.DataSource = dt快照

    End Sub

    Private Sub RefreshRightPositon()

        Dim totalWidth As Integer = 0

        totalWidth = dgTable.Columns.GetColumnsWidth(DataGridViewElementStates.Displayed)

        If dgTable.DisplayedRowCount(True) <> dgTable.RowCount Then
            totalWidth += 20
        Else
            totalWidth += 3
        End If
        SplitContainer3.SplitterDistance = totalWidth

    End Sub

    Private Sub dgLeft_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgLeft.SelectionChanged

        If dgLeft.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim sql As String = "select ID AS 编号,Key_Timer as 快照时间, U_TableName as 更新表名称,Table_UpdateTime as 更新表时间,TypeName as 更新类型 from " + MyConfig.前缀 + "_Table where Key_Timer=" & dgLeft.SelectedRows(0).Cells(0).Value
        Dim dt As DataTable = IListenDb.CommonConUtil.GetTable(sql)
        快照详细信息(dgLeft.SelectedRows(0).Cells(0).Value, dt)
        IListenDb.CommonConUtil.CloseCon()

        dt.Columns.Add("实际表名称")

        For index As Integer = 0 To dt.Rows.Count - 1
            dt.Rows(index)("实际表名称") = dt.Rows(index)("更新表名称").ToString().Replace(MyConfig.前缀 & "_", "")
        Next

        If dt.Rows.Count > 0 Then
            dt开始编号.Value = CDec(dt.Rows(0)("编号"))
            dt结束编号.Value = CDec(dt.Rows(dt.Rows.Count - 1)("编号"))
        End If

        Dim 所有组件 As List(Of 组件表) = 组件表操作类.GetAll组件
        Dim lstDataNew As IListenDb.IFormatMainForm
        If 所有组件.Count > 0 Then
            For index As Integer = 所有组件.Count - 1 To 0 Step -1
                lstDataNew = Assembly.Load(所有组件(index).组件名称).CreateInstance(所有组件(index).组件名称 & ".FormatMainForm")
                If lstDataNew IsNot Nothing Then
                    lstDataNew.RefreshRight(dt, dgTable)
                    Exit For
                End If
            Next
        End If

        _lastTable = ""
        RefreshRightPositon()

    End Sub

    Private _lastTable As String = ""
    Private Sub dgTable_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgTable.SelectionChanged

        If dgLeft.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If dgTable.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        '同一个表 不多次显示
        If _lastTable = dgTable.SelectedRows(0).Cells("更新表名称").Value Then
            Exit Sub
        End If

        dgData.DataSource = Nothing

        Dim updateTableName As String = dgTable.SelectedRows(0).Cells("更新表名称").Value
        updateTableName = updateTableName.Substring(MyConfig.前缀.Length + 1)

        Dim sql As String = "SELECT name FROM sys.columns WHERE object_id=Object_Id('" & updateTableName & "') and column_id in (select colid from sysindexkeys where id=Object_Id('" & updateTableName & "') and indid=1 )"
        Dim dt主键 As DataTable = IListenDb.CommonConUtil.GetTable(sql)

        sql = "select * from " & dgTable.SelectedRows(0).Cells("更新表名称").Value & " where " & MyConfig.前缀 & "_Key=" & dgLeft.SelectedRows(0).Cells(0).Value & " order by " + MyConfig.前缀 + "_UpdateTimer"

        Dim dt As DataTable = IListenDb.CommonConUtil.GetTable(sql)

        SetDataViewTable(dgData, 筛选(去掉触发器生成的数据_实际表(dt)))

        If chk隐藏行头.Checked = True Then
            For index As Integer = 0 To 3
                dgData.Columns(index).Visible = False
            Next
        End If

        For index As Integer = 0 To dgData.RowCount - 1
            dgData.Rows(index).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft
            Select Case CInt(Val(dgData.Rows(index).Cells(MyConfig.前缀 & "_Type").Value))
                Case 1
                    dgData.Rows(index).HeaderCell.Value = "添加" & "(" & CStr(dgData.Rows(index).Cells(MyConfig.前缀 & "_UpdateId").Value) & ")"
                Case 2
                    dgData.Rows(index).HeaderCell.Value = "修改前" & "(" & CStr(dgData.Rows(index).Cells(MyConfig.前缀 & "_UpdateId").Value) & ")"
                Case 3
                    dgData.Rows(index).HeaderCell.Value = "修改后" & "(" & CStr(dgData.Rows(index).Cells(MyConfig.前缀 & "_UpdateId").Value) & ")"
                Case 4
                    dgData.Rows(index).HeaderCell.Value = "删除" & "(" & CStr(dgData.Rows(index).Cells(MyConfig.前缀 & "_UpdateId").Value) & ")"
            End Select
        Next

        Dim lst主键索引 As New List(Of String)
        For index As Integer = 0 To dt主键.Rows.Count - 1
            For jdex As Integer = 0 To dgData.ColumnCount - 1
                If dgData.Columns(jdex).Name = dt主键.Rows(index)("name") Then
                    dgData.Columns(jdex).HeaderText = dgData.Columns(jdex).HeaderText & "(" & "K)"
                    dgData.Columns(jdex).DisplayIndex = lst主键索引.Count
                    dgData.Columns(jdex).Frozen = True
                    dgData.Columns(jdex).DefaultCellStyle.BackColor = Color.Green
                    lst主键索引.Add(dgData.Columns(jdex).Name)
                End If
            Next
        Next

        IListenDb.CommonConUtil.CloseCon()

        _lastTable = dgTable.SelectedRows(0).Cells("更新表名称").Value
    End Sub

    Private Sub tsb刷新_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb刷新.Click

        RefreshLeft()

    End Sub

    Private Sub tsb清除_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Function 去掉触发器生成的数据_实际表(ByVal 更新表 As DataTable) As DataTable

        Return 更新表

    End Function

    Private Function 筛选(ByVal dt As DataTable) As DataTable

        Dim rowFilter As String = ""
        rowFilter = "  " + MyConfig.前缀 + "_UpdateId >= " & dt开始编号.Value
        rowFilter += " AND " + MyConfig.前缀 + "_UpdateId <=" & dt结束编号.Value & " AND ( 1 = 2 "

        If chk添加.Checked Then
            rowFilter += " OR " + MyConfig.前缀 + "_Type=1"
        End If

        If rad变更前.Checked Then
            rowFilter += " OR " + MyConfig.前缀 + "_Type=2"
        End If

        If rad变更后.Checked Then
            rowFilter += " OR " + MyConfig.前缀 + "_Type=3"
        End If

        If rad删除.Checked Then
            rowFilter += " OR " + MyConfig.前缀 + "_Type=4"
        End If
        rowFilter += " OR 1 = 2 ) "

        Dim dv As DataView = dt.DefaultView
        dv.RowFilter = rowFilter

        Return dv.ToTable

    End Function

    Private Function 筛选1(ByVal dt As DataTable) As DataTable

        Dim rowFilter As String = ""
        rowFilter = "  编号 >= " & dt开始编号.Value
        rowFilter += " AND 编号 <=" & dt结束编号.Value & " AND ( 1 = 2 "

        If chk添加.Checked Then
            rowFilter += " OR 更新类型='添加'"
        End If

        If rad变更前.Checked Then
            rowFilter += " OR 更新类型='修改前'"
        End If

        If rad变更后.Checked Then
            rowFilter += " OR 更新类型='修改后'"
        End If

        If rad删除.Checked Then
            rowFilter += " OR 更新类型='删除'"
        End If
        rowFilter += " OR 1 = 2 ) "

        Dim dv As DataView = dt.DefaultView
        dv.RowFilter = rowFilter

        Return dv.ToTable

    End Function

    Private Sub btn筛选_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn筛选.Click

        Dim dt As DataTable = CType(dgTable.Tag, DataTable)

        dgTable.DataSource = 筛选1(dt)

    End Sub

    Private Sub 删除快照时间ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除快照时间ToolStripMenuItem.Click

        If dgLeft.RowCount = 1 Then

            wys.util.Common.MessageBase.ShowMessage("必须有一条数据")
            Exit Sub

        End If

        If wys.util.Common.MessageBase.ShowMessage(Me, "是否清除此快照?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

            If dgLeft.SelectedRows.Count = 0 Then
                Exit Sub
            End If

            Dim sql As String = "select Key_Timer as 快照时间 ,U_TableName as 更新表名称 from " + MyConfig.前缀 + "_Table where Key_Timer=" & dgLeft.SelectedRows(0).Cells(0).Value

            Dim dt As DataTable = IListenDb.CommonConUtil.GetTable(sql)

            If dt.Rows.Count.ToString Then

                For index As Integer = 0 To dt.Rows.Count - 1
                    sql = "delete from " & CStr(dt.Rows(index)("更新表名称")) & " Where " & MyConfig.前缀 & "_Key=" & dgLeft.SelectedRows(0).Cells(0).Value
                    IListenDb.CommonConUtil.ExecuteSql(sql)
                Next

                sql = "delete from " + MyConfig.前缀 + "_Table where Key_Timer=" & dgLeft.SelectedRows(0).Cells(0).Value
                IListenDb.CommonConUtil.ExecuteSql(sql)

            End If

            sql = "delete from " + MyConfig.前缀 + "_Key where Key_Timer=" & dgLeft.SelectedRows(0).Cells(0).Value
            IListenDb.CommonConUtil.ExecuteSql(sql)

            sql = "select max(id) from " + MyConfig.前缀 + "_Count"
            Dim dt2 As DataTable = IListenDb.CommonConUtil.GetTable(sql)
            If dt2 IsNot Nothing AndAlso dt2.Rows.Count > 0 Then
                Dim maxId As Decimal = CDec(dt2.Rows(0)(0))

                sql = " insert into " + MyConfig.前缀 + "_Count(id) values(" + maxId + ")"
                IListenDb.CommonConUtil.ExecuteSql(sql)
            End If

            sql = "delete max(id) from " + MyConfig.前缀 + "_Count"
            IListenDb.CommonConUtil.ExecuteSql(sql)

            dgTable.DataSource = dt
            dgTable.Tag = dgTable.DataSource

            IListenDb.CommonConUtil.CloseCon()

            RefreshLeft()

        End If

    End Sub

    Private Sub 清除快照数据ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 清除快照数据ToolStripMenuItem.Click

        If wys.util.Common.MessageBase.ShowMessage(Me, "是否清除此快照?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

            If dgLeft.SelectedRows.Count = 0 Then
                Exit Sub
            End If

            Dim sql As String = "select Key_Timer as 快照时间 ,U_TableName as 更新表名称 from " + MyConfig.前缀 + "_Table where Key_Timer=" & dgLeft.SelectedRows(0).Cells(0).Value

            Dim dt As DataTable = IListenDb.CommonConUtil.GetTable(sql)

            If dt.Rows.Count.ToString Then

                For index As Integer = 0 To dt.Rows.Count - 1
                    sql = "delete from " & CStr(dt.Rows(index)("更新表名称")) + " Where " & MyConfig.前缀 & "_Key=" & dgLeft.SelectedRows(0).Cells(0).Value
                    IListenDb.CommonConUtil.ExecuteSql(sql)
                Next

                sql = "delete from " + MyConfig.前缀 + "_Table where Key_Timer=" & dgLeft.SelectedRows(0).Cells(0).Value
                IListenDb.CommonConUtil.ExecuteSql(sql)

            End If

            sql = "select max(ID) from " + MyConfig.前缀 + "_Count"
            Dim dt2 As DataTable = IListenDb.CommonConUtil.GetTable(sql)
            Dim maxId As Decimal = CDec(dt2.Rows(0)(0))

            sql = "delete from " + MyConfig.前缀 + "_Count"
            IListenDb.CommonConUtil.ExecuteSql(sql)

            sql = " insert into " + MyConfig.前缀 + "_Count(ID) values(" & maxId & ")"
            IListenDb.CommonConUtil.ExecuteSql(sql)

            dgTable.DataSource = dt
            dgTable.Tag = dgTable.DataSource

            IListenDb.CommonConUtil.CloseCon()

            RefreshLeft()

        End If

    End Sub

    Private Sub btn新窗口_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn新窗口.Click

        Dim dt As DataTable = CType(dgData.Tag, DataTable)
        If dt Is Nothing OrElse dt.Rows.Count = 0 Then
            Exit Sub
        End If

        Dim newTable As DataTable = dt.Clone

        For index As Integer = 0 To dgData.Rows.Count - 1
            If dgData.Rows(index).Selected = True Then
                newTable.ImportRow(dt.Rows(index))
            End If
        Next

        Dim updateTableName As String = dgTable.SelectedRows(0).Cells("更新表名称").Value
        updateTableName = updateTableName.Substring(MyConfig.前缀.Length + 1)

        Dim sql As String = "SELECT name FROM sys.columns WHERE object_id=Object_Id('" & updateTableName & "') and column_id in (select colid from sysindexkeys where id=Object_Id('" & updateTableName & "') and indid=1 )"
        Dim dt主键 As DataTable = IListenDb.CommonConUtil.GetTable(sql)

        Dim frm As IListenDb.ListDataBase = 得到浏览数据窗体()
        If frm Is Nothing Then
            wys.util.Common.MessageBase.ShowMessage("没有数据浏览窗体")
            Exit Sub
        End If

        frm.隐藏行头 = chk隐藏行头.Checked
        frm.数据 = newTable
        frm.主键 = dt主键
        frm.前缀 = MyConfig.前缀

        frm.Show()

    End Sub

    Private Sub tsb设置组件_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb设置组件.Click

        Using frm As New AddinsStatus
            frm.ShowDialog(Me)
        End Using

    End Sub

    Private Sub 删除所有快照ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除所有快照ToolStripMenuItem.Click

        If wys.util.Common.MessageBase.ShowMessage(Me, "是否清除所有快照?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

            If dgLeft.RowCount = 0 Then
                Exit Sub
            End If
            Dim sql As String

            For jdex As Integer = 0 To dgLeft.RowCount - 1

                sql = "select Key_Timer as 快照时间 ,U_TableName as 更新表名称 from " + MyConfig.前缀 + "_Table where Key_Timer=" & dgLeft.Rows(jdex).Cells(0).Value
                Dim dt As DataTable = IListenDb.CommonConUtil.GetTable(sql)

                If dt.Rows.Count.ToString Then

                    For index As Integer = 0 To dt.Rows.Count - 1
                        sql = "delete from " & CStr(dt.Rows(index)("更新表名称")) + " Where " & MyConfig.前缀 & "_Key=" & dgLeft.Rows(jdex).Cells(0).Value
                        IListenDb.CommonConUtil.ExecuteSql(sql)
                    Next

                    sql = "delete from " + MyConfig.前缀 + "_Table where Key_Timer=" & dgLeft.Rows(jdex).Cells(0).Value
                    IListenDb.CommonConUtil.ExecuteSql(sql)

                End If

                sql = "select max(ID) from " + MyConfig.前缀 + "_Count"
                Dim dt2 As DataTable = IListenDb.CommonConUtil.GetTable(sql)
                Dim maxId As Decimal = CDec(dt2.Rows(0)(0))

                sql = "delete from " + MyConfig.前缀 + "_Count"
                IListenDb.CommonConUtil.ExecuteSql(sql)

                sql = " insert into " + MyConfig.前缀 + "_Count(ID) values(" & maxId & ")"
                IListenDb.CommonConUtil.ExecuteSql(sql)

            Next

            sql = "delete from " + MyConfig.前缀 + "_Key"
            IListenDb.CommonConUtil.ExecuteSql(Sql)

            sql = "Insert into " + MyConfig.前缀 + "_Key(Key_Timer,Descripion) values('" + Now.ToString("yyyyMMddHHmmss.fff") + "',N'王永胜数据库监控')"
            IListenDb.CommonConUtil.ExecuteSql(sql)

            IListenDb.CommonConUtil.CloseCon()

            RefreshLeft()

        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If MyConfig.自动刷新 = "1" Then

            Dim lastSelectIndex As Integer = Me.dgLeft.SelectedRows(0).Index

            Me.RefreshLeft()

            If Me.dgLeft.RowCount - 1 >= lastSelectIndex Then
                Me.dgLeft.Rows(lastSelectIndex).Selected = True
            End If

            Me.RefreshStatus()

        End If

    End Sub

    Private Sub tsb自动刷新_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb自动刷新.Click

        If MyConfig.自动刷新 = "1" Then
            MyConfig.自动刷新 = "0"
        Else
            MyConfig.自动刷新 = "1"
        End If

        If MyConfig.自动刷新 = "0" Then
            tsb自动刷新.ForeColor = Color.Red
        Else
            tsb自动刷新.ForeColor = System.Drawing.SystemColors.ControlText
        End If

    End Sub

    Private Sub chk隐藏行头_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk隐藏行头.CheckedChanged

        If chk隐藏行头.CheckAlign Then
            MyConfig.隐藏行头 = "1"
        Else
            MyConfig.隐藏行头 = "0"
        End If

    End Sub

    Private Sub btn所有数据_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn所有数据.Click

        Dim dt As DataTable = CType(dgData.Tag, DataTable)
        If dt Is Nothing OrElse dt.Rows.Count = 0 Then
            Exit Sub
        End If

        Dim newTable As DataTable = dt.Clone

        For index As Integer = 0 To dgData.Rows.Count - 1
            newTable.ImportRow(dt.Rows(index))
        Next

        Dim updateTableName As String = dgTable.SelectedRows(0).Cells("更新表名称").Value
        updateTableName = updateTableName.Substring(MyConfig.前缀.Length + 1)

        Dim sql As String = "SELECT name FROM sys.columns WHERE object_id=Object_Id('" & updateTableName & "') and column_id in (select colid from sysindexkeys where id=Object_Id('" & updateTableName & "') and indid=1 )"
        Dim dt主键 As DataTable = IListenDb.CommonConUtil.GetTable(sql)

        Dim frm As IListenDb.ListDataBase = 得到浏览数据窗体()
        If frm Is Nothing Then
            wys.util.Common.MessageBase.ShowMessage("没有数据浏览窗体")
            Exit Sub
        End If

        frm.隐藏行头 = chk隐藏行头.Checked
        frm.数据 = newTable
        frm.主键 = dt主键
        frm.前缀 = MyConfig.前缀
        frm.Show()

    End Sub

    Private Sub tsb比较快照_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb比较快照.Click
        Using frm As New CompareForm
            frm.隐藏行头 = chk隐藏行头.Checked
            frm.ShowDialog()
        End Using
    End Sub

    Private Sub dgData_Sorted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgData.Sorted
        For index As Integer = 0 To dgData.RowCount - 1
            Select Case CInt(Val(dgData.Rows(index).Cells(MyConfig.前缀 & "_Type").Value))
                Case 1
                    dgData.Rows(index).HeaderCell.Value = "添加" & "(" & CStr(dgData.Rows(index).Cells(MyConfig.前缀 & "_UpdateId").Value) & ")"
                Case 2
                    dgData.Rows(index).HeaderCell.Value = "修改前" & "(" & CStr(dgData.Rows(index).Cells(MyConfig.前缀 & "_UpdateId").Value) & ")"
                Case 3
                    dgData.Rows(index).HeaderCell.Value = "修改后" & "(" & CStr(dgData.Rows(index).Cells(MyConfig.前缀 & "_UpdateId").Value) & ")"
                Case 4
                    dgData.Rows(index).HeaderCell.Value = "删除" & "(" & CStr(dgData.Rows(index).Cells(MyConfig.前缀 & "_UpdateId").Value) & ")"
            End Select
        Next
    End Sub

    Private Function 得到浏览数据窗体() As IListenDb.ListDataBase

        Dim 所有组件 As List(Of 组件表) = 组件表操作类.GetAll组件

        Dim lstDataNew As IListenDb.ListDataBase
        If 所有组件.Count > 0 Then
            For index As Integer = 所有组件.Count - 1 To 0 Step -1
                If 所有组件(index).浏览数据窗体 = 1 Then
                    lstDataNew = Assembly.Load(所有组件(index).组件名称).CreateInstance(所有组件(index).组件名称 & ".ListData")
                    If lstDataNew IsNot Nothing Then
                        Return lstDataNew
                    End If
                End If
            Next
        End If

        Return Nothing

    End Function

    Private Sub SetDataViewTable(ByVal dv As DataGridView, ByVal dt As DataTable)

        'dv.DataSource = Nothing
        'dv.Columns.Clear()

        'If dt Is Nothing OrElse dt.Rows.Count = 0 Then
        '    Exit Sub
        'End If

        'dv.AutoGenerateColumns = False

        'Dim columnWidth As Integer = 100
        'Dim dc As DataGridViewTextBoxColumn

        'For index As Integer = 0 To dt.Columns.Count - 1
        '    dc = New DataGridViewTextBoxColumn
        '    dc.Name = dt.Columns(index).ColumnName
        '    dc.HeaderText = dt.Columns(index).ColumnName
        '    dc.DataPropertyName = dt.Columns(index).ColumnName

        '    Select Case dt.Columns(index).DataType

        '    End Select

        '    dv.Columns.Add(dc)
        'Next

        dgData.DataSource = 筛选(去掉触发器生成的数据_实际表(dt))
        dgData.Tag = CType(dgData.DataSource, DataTable).Copy

        'For col As Integer = 0 To dgData.Columns.Count - 1
        '    dgData.Columns(col).SortMode = DataGridViewColumnSortMode.NotSortable
        'Next
    End Sub

End Class
